/**
 * 
 */

window.onload = function () {
	
//------------------------------goods_find-------------------------------------------------

	var find = document.getElementById("find");
		
	/*find.onclick = function(){
		xmlRequest();
	}*/
	xmlRequest();
	function xmlRequest(){
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			var goodsTableHead="<table border='1' id='goods_table'>"+
			"<caption>商品表信息</caption>"+
			"<tr>"+
				"<th>选项</th>"+
				"<th>商品号</th>"+
				"<th>商品名</th>"+
				"<th>商品类型</th>"+
				"<th>商品单价（元）</th>"+
				"<th>操作</th>"+
			"</tr>";
			
			var goods="";
			var goodsTableTail="</table>";
			
			if(xhr.readyState==4){
				if(xhr.status >=200 && xhr.status <300 || xhr.status ==304 ){
					
					var goodsJSON =xhr.responseText;
					//console.log("servlet传来的字符串："+goodsJSON);
					
					if(goodsJSON!="0"){
						var goodsArray = JSON.parse(goodsJSON);
						
						//console.log("servlet传来的字符串,经过JSON拆开后的第一个数组元素0："+goodsArray[0].gId);

						for(var i=0;i<goodsArray.length;i++){
							if(i%2==0){
								goods+="<tr id='lightColor'>" +
										"<td><input type='checkbox' name='goods' value='"+goodsArray[i].gId+"' id=''/></td>" +
										"<td>"+goodsArray[i].gId+"</td>" +
										"<td>"+goodsArray[i].gName+"</td>" +
										"<td>"+goodsArray[i].gType+"</td>" +
										"<td>"+goodsArray[i].gPrice+"</td>"+
										"<td><a name='goodsOne' href='javascript:link(\"../goods_DeleteServlet?gIds=K "+goodsArray[i].gId+"\")' onclick=\"alert(\"您确定要删除"+goodsArray[i].gName+"吗？\");\">删除</a></td>"+	
									"</tr>";
							}else{
								goods+="<tr>" +
										"<td><input type='checkbox' name='goods' value='"+goodsArray[i].gId+"' id=''/></td>" +
										"<td>"+goodsArray[i].gId+"</td>" +
										"<td>"+goodsArray[i].gName+"</td>" +
										"<td>"+goodsArray[i].gType+"</td>" +
										"<td>"+goodsArray[i].gPrice+"</td>"+
										"<td><a name='goodsOne' href='javascript:link(\"../goods_DeleteServlet?gIds=K "+goodsArray[i].gId+"\")' onclick=\"alert(\"您确定要删除"+goodsArray[i].gName+"吗？\");\">删除</a></td>"+	
									"</tr>";
							}
						}
						console.log(goodsTableHead+goods+goodsTableTail);
						find.innerHTML=goodsTableHead+goods+goodsTableTail;
					}else{
						find.innerHTML = "<p>表信息空空如也--''</p>";
					}
				}else{
					alert("请求失败！");
					console.log("请求失败");
				}
			}
		}
		xhr.open("post","../goods_FindServlet",true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		xhr.send("find=allTables&b=3");
	}

		
//goods 删除-------------------------------------------------------
	var delete_goods = document.getElementById("delete");//删除按钮
	var selectAll = document.getElementById("checkAll");//查询所有按钮
	var cancel = document.getElementById("cancel");//取消按钮
	
	
//BEGIN__批量删除功能: 隐藏按钮实现，当按下 “批量删除” 按钮 ，后多出一个删除按钮，以确定删除
	var deleteBatch = document.getElementById("deleteBatch");//批量删除按钮

	
	deleteBatch.onclick = function(){
		delete_goods.style.visibility = "visible";
		cancel.style.visibility = "visible";
		//标签的“visibility”能见度属性：当它为hidden ，表示隐藏;为visible,可见	
	}
	cancel.onclick = function(){//恢复隐藏形式：在删除之后 见下
		delete_goods.style.visibility = "hidden";
		cancel.style.visibility = "hidden";
	}

	
	//jquerry 方法:详细见 隐藏按钮教程.html
	/*$("#deleteBatch").click(function(){
		$("#delete_goods").toggle();
	});*/
	
//END__批量删除功能
	
	//获取goodId函数
	function getGIds(){
		var gIds="";
		var goods = document.getElementsByName("goods");
		for(var i=0;i<goods.length;i++){
			if(goods[i].checked==true){//选中的GOODS
				gIds+=goods[i].value+" ";
			}
		}
		return gIds; 
	}

	
	//deleteGoods函数: 删除goods
	function deleteGoods(gIds){
		var goodsDeleteCount;
		//console.log("发送Ajax,你选择删除商品的Goods_Id:"+gIds);
		//ajax发送请求：把拿选中的gId拿到后，传给goods_DeleteServlet
		var xhr = new XMLHttpRequest();
		
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4){
				if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
					var deleteResult = xhr.responseText;
					console.log("deleteResult:个数"+deleteResult);
					if(deleteResult!="0"){
						goodsDeleteCount=deleteResult;
						console.log("Ajax:后端删除成功");
					}else{
						console.log("Ajax:后端删除失败");
					}
				}
			}
		}
		
		
		xhr.open("get","../goods_DeleteServlet?gIds="+gIds,true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		
		//send：为了servlet好接入参数，并不能直接给gIds，填入以 "'参数名'=参数" 为格式的“字符串”参数
		//xhr.send(gIds); 这种发送方式失败！
		//console.log("gIds="+gIds); get方式发送参数不存在这写，一定要反在url后以？加参数形式
		xhr.send();
		
		//Ajax 函数完毕
		console.log("返回goodsDeleteCount:"+goodsDeleteCount);
		return goodsDeleteCount;
	}
	
	//多个或一个 删除商品功能
	delete_goods.onclick = function(){
		var gIds = getGIds();//获取选中gId,拿到一个数组
		if(gIds!=0){
			//点击弹出确定框
			var dlt = confirm("您确定要删除这个商品吗？");
			//判读是，则删除；否则，返回
			if(dlt==true){
				if(gIds.length!=0){//有商品选择了
					//调用删除
					var dtgoods = deleteGoods(gIds);
					console.log("dtgoods:"+dtgoods);
					//返回“删除成功！”
					if(dtgoods!=0){
						alert("删除成功！");
						xmlRequest();
					}else{
						alert("删除失败！");
					}
				}else{
					alert("请先勾选您要删除的商品!");
				}
			}
		}else{
			alert("请先勾选您要删除的商品!");
		}
		
	}
	// 删除一个特定的商品
	 function deleteByOne(gName,gId){
		 console.log(gName+","+gId);
		if(gName!="" && gId!=""){
			//点击弹出确定框
			var dlt = confirm("您确定要删除'"+gName+"'吗？");
			//判读是，则删除；否则，返回
			if(dlt==true){
					//调用删除
					var dtgoods = deleteGoods(gId);
					console.log("dtgoods:"+dtgoods);
					//返回“删除成功！”
					if(dtgoods!=0){
						alert("删除成功！");
						xmlRequest();
					}else{
						alert("删除失败！");
					}
			}
		}else{
			alert("请先勾选您要删除的商品!");
		}
	 }
	 
	 
	//全选获取gId功能
	selectAll.onclick = function(){
		var ipts = document.getElementsByTagName("input");
		for(var i=0;i<ipts.length;i++){
			if(ipts[i].type=="checkbox"){
				ipts[i].checked=!ipts[i].checked;
			}
		}
		selectAll.checked =!selectAll.checked;
	}	
}